﻿using System;
using System.Collections.Generic;

using WAF.EntityFramework;
using CtpTrader.Logic.Entity;

namespace CtpTrader.Logic.Core.Common
{
    /// <summary>
    /// 合约管理类
    /// </summary>
    public class InstrumentManager
    {
        /// <summary>
        /// 单例模式对象
        /// </summary>
        private static InstrumentManager instance;

        /// <summary>
        /// 合约列表
        /// </summary>
        private Instrument[] instruments;

        /// <summary>
        /// 订阅了行情的合约
        /// </summary>
        private string[] subscribed;

        /// <summary>
        /// 构造函数
        /// </summary>
        private InstrumentManager()
        {
            subscribed = new string[]{ "ru1305" };

            instruments = EntityAction.Select<Instrument>("SELECT * FROM Instrument");
        }

        /// <summary>
        /// 获取单例模式对象
        /// </summary>
        /// <returns>单例模式对象</returns>
        public static InstrumentManager GetInstance()
        {
            if (instance == null)
                instance = new InstrumentManager();

            return instance;
        }
        /// <summary>
        /// 取得订阅了行情的合约列表
        /// </summary>
        /// <returns></returns>
        public string[] GetSubscribe()
        {
            return subscribed;
        }

        /// <summary>
        /// 根据合约代码查找合约对象
        /// </summary>
        /// <param name="instrumentID">合约代码</param>
        /// <returns>合约对象</returns>
        public Instrument Find(string instrumentID)
        {
            foreach (Instrument instrument in this.instruments)
            {
                if (instrument.InstrumentID == instrumentID)
                    return instrument;
            }

            return null;
        }
    }
}
